const Service = require('egg').Service;
const { error, success } = require('../utils/response.js')
const moment = require('moment')

class LikeService extends Service {
    // 点赞评论
    async addLike(info, ctx) {
        const token = ctx.request.header.authorization;
        const userInfo = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);
        const { type } = info;
        const id = Number(info.id);
        const idName = type + 'Ids'
        let tableName;
        switch (type) {
            case 'inv':
                tableName = 'invitation'
                break;
            case 'com':
                tableName = 'comment'
                break;
            case 'art':
                tableName = 'article'
                break;
            case 'artcom':
                tableName = 'articlecom'
                break;
            default:
                break;
        }
        // 更新用户喜欢表
        const userlike = await this.app.mysql.get('userlike', { userId: userInfo.id })
        let likeArr = userlike[idName]
        if (!likeArr) {
            // 还没有数据
            likeArr = [];
        } else {
            likeArr = JSON.parse(likeArr);
        }
        if (likeArr.includes(id)) return error(ctx, '已经点过赞了!')
        likeArr.push(id)
        console.log(likeArr);
        likeArr = JSON.stringify(likeArr)
        const res1 = await this.app.mysql.query(`update userlike set ${idName}=? where userId=?`, [likeArr, userInfo.id]);
        if (res1.affectedRows !== 1) return error(ctx, `点赞失败`);
        // 更新表内的点赞数
        const res = await this.app.mysql.query(`update ${tableName} set likes=likes+1 where id=?`, [id]);
        if (res.affectedRows !== 1) return error(ctx, `更新${tableName}表likes失败`);
        success(ctx, '点赞成功')
    }
    // 获取用户like列表
    async userlike(info, ctx) {
        const token = ctx.request.header.authorization;
        const userInfo = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);
        const res = await this.app.mysql.get('userlike', { userId: userInfo.id });
        success(ctx, '获取成功', res)
    }
    // 取消点赞
    async removelike(info, ctx) {
        const token = ctx.request.header.authorization;
        const userInfo = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);
        const { type } = info;
        const id = Number(info.id);
        const idName = type + 'Ids'
        let tableName;
        switch (type) {
            case 'inv':
                tableName = 'invitation'
                break;
            case 'com':
                tableName = 'comment'
                break;
            case 'art':
                tableName = 'article'
                break;
            case 'artcom':
                tableName = 'articlecom'
                break;
            default:
                break;
        }
        // 更新用户喜欢表
        const userlike = await this.app.mysql.get('userlike', { userId: userInfo.id })
        let likeArr = userlike[idName]
        if (!likeArr) {
            // 还没有数据
            return error(ctx,'还未点赞，不可取消点赞')
        } else {
            likeArr = JSON.parse(likeArr);
        }
        likeArr.splice(likeArr.indexOf(id), 1)
        likeArr = JSON.stringify(likeArr)
        const res1 = await this.app.mysql.query(`update userlike set ${idName}=? where userId=?`, [likeArr, userInfo.id]);
        if (res1.affectedRows !== 1) return error(ctx, `取消点赞失败`);
        // 更新表内的点赞数
        const res = await this.app.mysql.query(`update ${tableName} set likes=likes-1 where id=?`, [id]);
        if (res.affectedRows !== 1) return error(ctx, `更新${tableName}表likes失败`);
        success(ctx, '取消点赞成功')
    }
}
module.exports = LikeService;